<?php

namespace catchAdmin\model;

use catcher\exceptions\AlertErrorException;

class UserExt extends ModelApp
{

    protected $append = [
        'audit_time_show',
        'company_status_name',
        'role_name',
        'sex_name',
        'job_type_name',
    ];


    protected $type = [
        'role' => 'array',
    ];

    ############################ 关系 start ###############################

    ############################ 关系 end ###############################
    ############################ Attr start ###############################

    /**
     * role_name
     */
    public function getRoleNameAttr()
    {
        $list = [
            1 => '居间人',
            2 => '电力行业从业者',
            3 => '求职者',
        ];

        $role_name = [];
        foreach ($this->role as $k => $v) {
            $role_name[] = $list[$v] ?? '';
        }

        return implode('，', $role_name);
    }


    /**
     * sex_name
     */
    public function getSexNameAttr()
    {
        $list = [
            1 => '男',
            2 => '女',
        ];

        return $list[$this->getOrigin('sex')] ?? '';
    }


    /**
     * job_type_name
     */
    public function getJobTypeNameAttr()
    {
        $list = [
            1 => '全职',
            2 => '兼职',
        ];

        return $list[$this->getOrigin('job_type')] ?? '';
    }


    /**
     * audit_time_show
     */
    public function getAuditTimeShowAttr()
    {
        if (empty($this->getOrigin('audit_time'))) {
            return '';
        }

        return date('Y-m-d H:i:s', $this->getOrigin('audit_time'));
    }


    /**
     * city_code
     */
    public function getCityCodeAttr()
    {
        return [
            substr($this->getOrigin('city_code'), 0, 2) . "0000",
            substr($this->getOrigin('city_code'), 0, 4) . "00",
            $this->getOrigin('city_code'),
        ];
    }


    /**
     * company_status_name_list
     */
    public function getCompanyStatusNameList()
    {
        return [
            2 => '审核中',
            3 => '审核通过',
            4 => '审核未通过',
        ];
    }


    /**
     * company_status_name
     */
    public function getCompanyStatusNameAttr()
    {
        return $this->getCompanyStatusNameList()[$this->getOrigin('company_status')] ?? '';
    }

    ############################ Attr end ###############################
    ############################ search start ###############################

    ############################ search end ###############################

    public function saveDataApp($param)
    {
        $msg_arr = [
            'wechat_user_id'      => $this->getLoginId(),
            'company_status'      => 2,
            'company_status_note' => '',
            'apply_time'          => time(),
            'audit_time'          => time(),
            'status_no'           => date('Ymd_') . rand(100, 999),

            'parent_id'    => $param['parent_id'] ?? 0,
            'role'         => $param['role'] ?? [],
            'realname'     => $param['realname'] ?? '',
            'work_company' => $param['work_company'] ?? '',
            'duty'         => $param['duty'] ?? '',
            'sex'          => $param['sex'] ?? 0,
            'xue_li'       => $param['xue_li'] ?? '',
            'university'   => $param['university'] ?? '',
            'mobile'       => $param['mobile'] ?? '',
            'email'        => $param['email'] ?? '',
            'city_code'    => $param['city_code'] ?? [],

            'join_is'        => $param['join_is'] ?? 0,
            'join_gong_xian' => $param['join_gong_xian'] ?? '',
            'join_why'       => $param['join_why'] ?? '',

            'ye_wu' => $param['ye_wu'] ?? '',

            'job_zhuan_chang' => $param['job_zhuan_chang'] ?? '',
            'job_school'      => $param['job_school'] ?? '',
            'job_history'     => $param['job_history'] ?? '',
            'job_want'        => $param['job_want'] ?? '',
            'job_type'        => $param['job_type'] ?? '',
            'job_salary'      => $param['job_salary'] ?? '',
            'job_introduce'   => $param['job_introduce'] ?? '',
        ];

        if (!empty($param['birthday'])) {
            $msg_arr['birthday'] = $param['birthday'];
        }

        // 城市名
        if (!empty($param['city_code']) && is_array($param['city_code'])) {
            $name_arr = [];
            foreach ($param['city_code'] as $key => $value) {
                $name_arr[] = (new Area())
                    ->where('code', $value)
                    ->value('name');
            }

            $msg_arr['city_code'] = end($param['city_code']);
            $msg_arr['city_name'] = implode('', $name_arr);
        }

        // 上级
        if (!empty($param['parent_id'])) {
            $parent_msg = (new WechatUser())
                ->where('id', $param['parent_id'])
                ->find();
            if (empty($parent_msg)) {
                throw new AlertErrorException('推广人不存在');
            }

            $msg_arr['parent_name'] = $parent_msg['name'];
            $msg_arr['parent_share_code'] = $parent_msg['share_code'];
        }

        $msg = $this
            ->where('wechat_user_id', $msg_arr['wechat_user_id'])
            ->find();
        if (!empty($msg)) {
            if ($msg['company_status'] == 3) {
                throw new AlertErrorException('已通过的审核，不允许修改');
            }
            if ($msg['company_status'] == 2) {
                throw new AlertErrorException('正在审核中，不允许修改');
            }

            $msg_arr['id'] = $msg['id'];
        }

        return parent::saveDataCC($msg_arr); // TODO: Change the autogenerated stub
    }

}
